package net.stratusprogramming.school.Game.World;

import net.stratusprogramming.school.Game.Framework.Scene.Actor;
import net.stratusprogramming.school.Game.Framework.System.InstanceMgr;
import net.stratusprogramming.school.Game.Framework.System.SaveMgr;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Rectangle;

public class Player extends Actor {

	private static final float SPEED = 0.10f;
		
	private Input input;
	private Map map;
	
	private int dir = 0; // down left right up
	private SpriteSheet sprite;
	private Animation[] anim;
	private Rectangle colissionRect;
	
	public Player() {
		map = InstanceMgr.map;
		input = InstanceMgr.input;
		try {
			sprite = new SpriteSheet(new Image("res/images/sprites/player.png"), 32, 32);
			anim = new Animation[4];
			anim[0] = new Animation(sprite, 0, 0, 2, 0, true, 200, true);
			anim[1] = new Animation(sprite, 0, 1, 2, 1, true, 200, true);
			anim[2] = new Animation(sprite, 0, 2, 2, 2, true, 200, true);
			anim[3] = new Animation(sprite, 0, 3, 2, 3, true, 200, true);
		} catch (SlickException e) {
			e.printStackTrace();
		}
		colissionRect = new Rectangle(0, 0, 32, 32);
	}
		
	@Override
	public void update(int delta) {
		float dist = SPEED * delta;
		if (input.isKeyDown(Input.KEY_UP))  {
			dir = 3;
			colissionRect.setX(x);
			colissionRect.setY(y - dist);
			if (!map.isBlocked(colissionRect))
				y -= dist;	   			
			if(anim[dir].isStopped()) anim[dir].start();			
		} else if (input.isKeyDown(Input.KEY_LEFT)) {
			dir = 1;
			colissionRect.setX(x - dist);
			colissionRect.setY(y);
			if(!map.isBlocked(colissionRect))
	    		x -= dist;
	    	if(anim[dir].isStopped()) anim[dir].start();
	    } else if (input.isKeyDown(Input.KEY_DOWN)) {
	    	dir = 0;
	    	colissionRect.setX(x);
	    	colissionRect.setY(y + dist);
	    	if(!map.isBlocked(colissionRect))
	    		y += dist;	      
	        if(anim[dir].isStopped()) anim[dir].start();
	    } else if (input.isKeyDown(Input.KEY_RIGHT)) {
	        dir = 2;
	        colissionRect.setX(x + dist);
	        colissionRect.setY(y);
	        if(!map.isBlocked(colissionRect))
	        	x += dist;	        
	        if(anim[dir].isStopped()) anim[dir].start();
	    } else {
	    	anim[dir].stop();
	    }
	}

	@Override
	public void render(Graphics g) {
		anim[dir].draw(x, y);
	}

	@Override
	public void save(SaveMgr saveMgr) {
		saveMgr.setSetting("player-x", x);
		saveMgr.setSetting("player-y", y);
		saveMgr.setSetting("player-dir", dir);
	}

	@Override
	public void load(SaveMgr saveMgr) {
		if(saveMgr.hasSetting("player-x")) x = saveMgr.getSettingFloat("player-x");
		if(saveMgr.hasSetting("player-y")) y = saveMgr.getSettingFloat("player-y");		
		if(saveMgr.hasSetting("player-dir")) dir = saveMgr.getSettingInt("player-dir");
	}

}
